﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;

public partial class Admin_shop_UpImgDemo : AdminBase
{
    public string imgArr = "", imgTxtArr="";
        public int _Id = 0;

    protected void Page_Load(object sender, EventArgs e)
    {



        Response.CacheControl = "no-cache";

        if (!string.IsNullOrEmpty(Request.Params["ID"]))
        {
            _Id = int.Parse(HttpContext.Current.Request.QueryString["ID"]);

        }

        if (!Page.IsPostBack)
        {
            SCClassTreeBind("商品一级分类...", this.aClassddl);

            StoreTreeBindFV("属于商家...","0", this.childClassId);


            if (_Id != 0)
            {
                ShowInfo(_Id);
            }
        }
    }

    //保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if ((this.aClassddl.SelectedValue)=="0")
        {
            Response.Write("<script> var Error=1;var Msg='请填写商品分类';</script>");
            return;
        }
   

        Entities mydb = new Entities();
        {
            S_Goods lx = new S_Goods();
            if (_Id != 0)
                lx = mydb.S_Goods.First(p => p.GID == _Id);

            lx.SCCLASSID = int.Parse(this.aClassddl.SelectedValue);
            //lx.CodeClassID = int.Parse(this.DropListCode.SelectedValue);
            lx.GImg = xsimg.Value.Trim();//图片

            lx.GName = txtName.Text.Trim();//名字
      
            lx.GContent = txtContent.Value;//详细
            lx.Gdw = Gdw.Text;

            lx.GPriceYJ= Convert.ToDecimal(GPriceYJ.Text.Trim());//市场价格
            lx.GPrice =Convert.ToDecimal(SPrice.Text.Trim());//商城价
            
            lx.GPrice2 = Convert.ToDecimal(SPrice2.Text.Trim());//会员价
            lx.GPrice3 = Convert.ToDecimal(SPrice3.Text.Trim());//邮费
          
            //lx.FXPrice = Convert.ToDecimal(fxPrice.Text.Trim()); ; //分销价格
            //lx.FXPrice2 = Convert.ToDecimal(fxPrice2.Text.Trim()); ; //分销价格
            //lx.FXPrice3 = Convert.ToDecimal(fxPrice3.Text.Trim()); ; //分销价格

            lx.GCounts = Convert.ToInt32(GCounts.Text.Trim());//库存
            lx.G_Introduce = txtKeyword.Text.Trim();//介绍
            lx.IsLock = true;
            lx.GSort = Convert.ToInt32(GSort.Text.Trim());
            lx.GSales = Convert.ToInt32(GSales.Text.Trim());

            //if (CBLog.Checked)
            //    lx.GvipLeve = 6;
            //else
            //    lx.GvipLeve =0;

            //if (CBxn.Checked)
            //    lx.GPost = "9";
            //else
            //    lx.GPost ="0";

            //lx.GArea = Convert.ToInt32(DropDownList1.SelectedValue);


            if (_Id == 0)
            {
                mydb.S_Goods.Add(lx);
                mydb.SaveChanges();
                Response.Write("<script> var success=1;msg='发布成功'</script>");
            }
            else
            {
                mydb.SaveChanges();
                Response.Write("<script> var success=2;var msg='修改成功';</script>");
            }
        }
    }

    private void ShowInfo(int _id)
    {

        Entities mydb = new Entities();
        {
            S_Goods _lx = mydb.S_Goods.First(p => p.GID == _id);
            txtName.Text = _lx.GName;
             txtKeyword.Text = _lx.G_Introduce;
          

            imgArr = getImg(_lx.GImg);

            txtContent.Value = _lx.GContent;

            GPriceYJ.Text = _lx.GPriceYJ.ToString();//商品市场价
            SPrice.Text = _lx.GPrice.ToString();//商城价
            SPrice2.Text = _lx.GPrice2.ToString();//会员价
            SPrice3.Text = _lx.GPrice3.ToString();//邮费
           
            //fxPrice.Text = _lx.FXPrice.ToString(); //分销价格
            //fxPrice2.Text = _lx.FXPrice2.ToString(); //分销价格
            //fxPrice3.Text = _lx.FXPrice3.ToString(); //分销价格

            GCounts.Text = _lx.GCounts.ToString();
            GSort.Text = _lx.GSort.ToString();
            GSales.Text = _lx.GSales.ToString();
            Gdw.Text= _lx.Gdw;
            //if (_lx.GvipLeve != 6)
            //CBLog.Checked = false;

            aClassddl.SelectedValue = _lx.SCCLASSID.ToString();
            //this.DropListCode.SelectedValue = _lx.CodeClassID.ToString();

            //DropDownList1.SelectedValue = _lx.GArea.ToString();
        }
    }



    /// <summary>
    /// 绑定类别DropDownList控制
    /// </summary>
    /// /// <param name="firstItemTxt">第一项显示的文字</param>
    /// <param name="ddl">要绑定的DropDownList控件</param>

    public void SCClassTreeBind(string firstItemTxt, DropDownList dp)
    {
        string tepmValue = "0";
        SCClassTreeBindFV(tepmValue, firstItemTxt, dp);

    }
    public void SCClassTreeBindFV(string firstItemValue, string firstItemTxt, DropDownList dp)
    {
        dp.Items.Clear();
        ListItem list = new ListItem(firstItemTxt, firstItemValue);
        dp.Items.Add(list);

        using (Entities _class = new Entities())
        {
            int a;
            int.TryParse(list.Value, out a);

            var m = from n in _class.S_Classs where n.SCpareID == a select n;
            foreach (var n in m)
            {
                dp.Items.Add(new ListItem(n.SCNmae.ToString(), n.SCID.ToString()));
                int leng = 1;
                dpbinder(dp, int.Parse(n.SCID.ToString()), leng);
            }
        }
    }



    public void StoreTreeBindFV(string firstItemValue, string firstItemTxt, DropDownList dp)
    {
        dp.Items.Clear();
        ListItem list = new ListItem(firstItemTxt, firstItemValue);
        dp.Items.Add(list);

        using (Entities _class = new Entities())
        {
            int a;
            int.TryParse(list.Value, out a);

            var m = from n in _class.S_Store select n;
            foreach (var n in m)
            {
                dp.Items.Add(new ListItem(n.Sname.ToString(), n.SID.ToString()));
            }
        }
    }





}
